package org.apache.ibatis.session;

import org.apache.ibatis.cursor.Cursor;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * The primary Java interface for working with MyBatis.
 * 使用MyBatis的主要Java接口。
 * Through this interface you can execute commands, get mappers and manage transactions.
 * 通过这个接口你可以执行命令、获取映射器和管理事务。
 *
 * @author Clinton Begin
 * @worshiper MNIQXH
 */
public interface SqlSession extends Closeable {
    /**
     * Retrieve a single row mapped from the statement key.
     * 使用语句检索单行映射
     * @param <T> the returned object type
     * @param statement
     *          the statement
     * @return Mapped object
     */
    <T> T selectOne(String statement);

    /**
     * Retrieve a single row mapped from the statement key and parameter
     * 使用语句和参数检索单行映射
     * @param <T> the returned object type
     * @param statement Unique identifier matching the statement to use.
     *                  匹配使用语句的唯一标识符
     * @param parameter A parameter object to pass to the statement.
     *                  传递给语句的参数对象
     * @return Mapped object
     */
    <T> T selectOne(String statement, Object parameter);

    /**
     * Retrieve a list of mapped objects from the statement key.
     * 使用语句检索列表对象映射
     * @param <E> the returned list element type
     *           返回列表元素类型
     * @param statement Unique identifier matching the statement to use.
     * @return List of mapped object
     */
    <E> List<E> selectList(String statement);

    /**
     * Retrieve a list of mapped objects from the satatement key and parameter.
     * 使用语句和参数检索列表对象映射
     * @param <E> the returned list element type
     * @param statement Unique identifier matching the statement to use.
     * @param parameter A parameter object to pass to the statement.
     * @return List of mapped object
     */
    <E> List<E> selectList(String statement, Object parameter);

    /**
     * Retrieve a list of mapped objects from the statement key and parameter,
     * within the specified row bounds.
     * 在指定的行边界内，使用语句和参数检索列表对象映射
     * @param <E> the returned list element type
     * @param statement Unique identifier matching the statement to use.
     * @param parameter A parameter object to pass to statement.
     * @param rowBounds Bounds to limit object retrieval
     *                  限制对象获取的边界
     * @return List of mapped object
     */
    <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);

    /**
     * The selectMap is a special case in that it is designed to convert a list
     * of results into Map based on one of the properties in the resulting
     * objects.
     * selectMap是一种特殊的情况，被设计为基于结果对象的一个属性将结果列表转换为Map
     * Eg. Return a of Map[Integer, Author] for selectMap("selectAuthors","id")
     * @param <K> the returned Map keys type
     * @param <V> the returned Map values type
     * @param statement Unique identifier matching the statement to use.
     * @param mapKey the property to use as key for each value in the list.
     *               列表中每一个值中作为key的属性名称
     * @return Map containing key pair data.
     */
    <K, V> Map<K, V> selectMap(String statement, String mapKey);

    /**
     * The selectMap is a special case in that it is designed to convert a list
     * of results into Map based on one of the properties in the resulting
     * objects.
     * @param <K> the returned Map keys type
     * @param <V> the returned Map values type
     * @param statement Unique identifier matching the statement to use.
     * @param parameter A parameter object to pass to the statement.
     * @param mapKey The property to use as key for each value in the list.
     * @return Map containing key pair data.
     */
    <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);

    /**
     * The selectMap is a special case in that it is designed to convert a list
     * of results into Map based on one of the properties in the resulting
     * objects.
     * @param <K> the returned Map keys type
     * @param <V> the returned Map values type
     * @param statement Unique identifier matching the statement to use.
     * @param parameter A parameter object to pass to the statement.
     * @param mapKey The property to use as key for each value in the list.
     * @param rowBounds Bounds to limit object retrieval
     * @return Map containing key pair data.
     */
    <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);

    /**
     * A Cursor offers the same result as a List, except it fetches data lazily using an Iterator.
     * 游标提供了和列表相同的结果，只是游标通过迭代器惰性的获取结果。
     * @param <T> the returned cursor element type.
     * @param statement Unique identifier matching the statement to use.
     * @return Cursor of mapped objects
     */
    <T> Cursor<T> selectCursor(String statement);

    /**
     * A Cursor offers the same results as a List, except it fetches data lazily using an iterator.
     * @param <T> the returned cursor element type.
     * @param statement Unique identifier matching the statement to use.
     * @param parameter A parameter object to pass to the statement.
     * @return Cursor of mapped objects
     */
    <T> Cursor<T> selectCursor(String statement, Object parameter);

    /**
     * A Cursor offers the same results as a List, except it fetches data lazily using an iterator.
     * @param <T> the returned cursor element type.
     * @param statement Unique identifier matching the statement to use.
     * @param parameter A parameter object to pass to the statement.
     * @param rowBounds Bounds to limit object retrieval.
     * @return Cursor of mapped objects
     */
    <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds);

    /**
     * Retrieve a single row mapped from the statement key and parameter
     * using a {@code ResultHandler}.
     * 使用ResultHandler通过statement和parameter检索单行映射。
     * @param statement Unique identifier matching the statement to use.
     * @param parameter A parameter object to pass to the statement.
     * @param handler ResultHandler that to pass each retrieved row
     */
    void select(String statement, Object parameter, ResultHandler handler);

    /**
     * Retrieve a sing row mapped from the statement key
     * using a {@code ResultHandler}
     * @param statement Unique identifier matching the statement to use.
     * @param handler ResultHandler that to pass each retrieved row
     */
    void select(String statement, ResultHandler handler);

    /**
     * Retrieve a single row mapped from the statement key and parameter using a {@code ResultHandler} and
     * {@code RowBounds}.
     * @param statement
     *          Unique identifier matching the statement to use.
     * @param parameter
     *          the parameter
     * @param rowBounds
     *          RowBounds instance to limit the query result
     * @param handler
     *          ResultHandler that to pass each retrieved row
     */
    void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);

    /**
     * Execute an insert statement.
     * 执行一条插入语句
     * @param statement Unique identifier matching the statement to execute.
     * @return int The number of rows affected by the insert.
     *          返回通过插入操作受影响的行数
     */
    int insert(String statement);

    /**
     * Execute an insert statement with the given parameter object. Any generated
     * autoincrement values or selectKey entries will modify the given parameter
     * object properties. Only the number of rows affected will be returned.
     * 使用给定的参数对象执行一条插入语句。任何生成自动增量的值或者selectKey条目都会修改给定
     * 的参数对象的值。只返回有影响的行数。
     * @param statement Unique identifier matching the statement to execute.
     * @param parameter A parameter object to pass to the statement.
     * @return int The number of rows affected by the insert.
     */
    int insert(String statement, Object parameter);

    /**
     * Execute an update statement. The number of rows affected will be returned.
     * @param statement Unique identifier matching the statement to execute.
     * @return int The number of rows affected by the update.
     */
    int update(String statement);

    /**
     * Execute an update statement. The number of rows affected will be returned.
     * @param statement Unique identifier matching the statement to execute.
     * @param parameter A parameter object to pass to the statement.
     * @return int The number of rows affected by the update.
     */
    int update(String statement, Object parameter);

    /**
     * Execute a delete statement. The number of rows affected will be returned.
     * @param statement Unique identifier matching the statement to execute.
     * @return int The number of rows affected by the delete.
     */
    int delete(String statement);

    /**
     * Execute a delete statement. The number of rows affected will be returned.
     * @param statement Unique identifier matching the statement to execute.
     * @param parameter A parameter object to pass to the statement.
     * @return int The number of rows affected by the delete.
     */
    int delete(String statement, Object parameter);

    /**
     * Flushes batch statements and commits database connection.
     * Note that database connection will not be committed if no updates/deletes/inserts were called.
     * To force the commit call {@link SqlSession#commit(boolean)}
     * 刷新批处理语句并提交数据库连接。
     * 注意：如果没有调用更新、删除、插入，则不会提交数据库连接。
     * 强制提交调用SqlSession#commit(boolean)
     */
    void commit();

    /**
     * Flushes batch statements and commits database connection.
     * @param force forces connection commit
     *              强制连接提交
     */
    void commit(boolean force);

    /**
     * Discards pending batch statement and rolls database connection back.
     * Note that database connection will not be rolled back if no updates/deletes/inserts were called.
     * To force the rollback call {@link SqlSession#rollBack(boolean)}
     */
    void rollBack();

    /**
     * Discards pending batch statement and rolls database connection back.
     * Note that database connection will not be rolled back if no updates/deletes/inserts were called.
     * 丢弃被挂起的批处理语句并且回滚数据库连接。
     * 注意：如果没有调用更新、删除、插入，则不会回滚数据库连接。
     * @param force force connection rollback
     */
    void rollBack(boolean force);

    /**
     * Flushes batch statements.
     * @return BatchResult list of updated records
     * @since 3.0.6
     */
    // TODO SqlSession->flushStatements
    //List<BatchResult> flushStatements();


    /**
     * Closes the session
     */
    @Override
    void close();

    /**
     * Clears local session cache.
     */
    void clearCache();

    /**
     * Retrieve current configuration.
     * @return Configuration
     */
    Configuration getConfiguration();

    /**
     * Retrieve a mapper.
     * @param <T> the mapper type
     * @param type Mapper interface class
     * @return a mapper bound to this SqlSession
     */
    <T> T getMapper(Class<T> type);

    /**
     * Retrieve inner database connection.
     * @return Connection
     */
    Connection getConnection();
}
